﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace sahcpims
{
    public partial class addMemberForm : Form
    {
        public addMemberForm()
        {
            InitializeComponent();
        }

        logIn logout = new logIn();

        public int employeeNumber;
        public string employeeName;
        public int familyNumber = 6;

        public int doFlag = 0;

        public void putAccountInfo()
        {
            familyNumberMismo.Text = familyNumber.ToString();
            empNumLabel.Text = employeeNumber.ToString();
            employeeLabel.Text = employeeName;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        private void logOutButton_Click(object sender, EventArgs e)
        {
            logout.Visible = true;
            this.Visible = false;
        }

        private void acceptMemberButton_Click(object sender, EventArgs e)
        {
            /** Connection String: Provider of the connection and the source of data **/
            string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\SAHCPIMS_database.mdb";

            /** create instance of the connection with the connection string as parameter **/
            OleDbConnection conn = new OleDbConnection(conString);

            /** 
             * Creating an instance of the DataSet which represents an in-memory cache of data
             * This is where the result of the database query will be stored
            **/
            DataSet ds = new DataSet();

            conn.Open();

            //for connection to the database
            try
            {
                OleDbDataAdapter tagalagay = new OleDbDataAdapter("INSERT into FamilyMember "
                                          + "values('" + familyNumber + "','" + memberFNameText.Text.ToString() + "','" + memberMNameText.Text.ToString() + "','" + memberSNameText.Text.ToString() + "','" + monthComboBox.Text.ToString() + "/" + dayComboBox.Text.ToString() + "/" + yearComboBox.Text.ToString() + "','" + int.Parse(weightTextBox.Text) + "','" +checkUpTypeCombo.Text.ToString() + "')",conn);

                tagalagay.Fill(ds);
                doFlag = 0;

            }
            //catches exception if the user input is invalid
            catch (FormatException)
            {

                memberVerificationLabel.Text = "Message: Invalid Input. Try Again.";
                doFlag = 1;

            }
            //catches exception if the input is already in the database or the input is not saved in the database
            catch (OleDbException)
            {
                memberVerificationLabel.Text = "Message: Data not Saved in Database. Data Already Exists.";
                doFlag = 1;

            }

             //closes database
                conn.Close();

                //if registration is successful, the form is disabled
                if (doFlag == 0)
                {

                    if (checkUpTypeCombo.Text.ToString() == "Prenatal")
                    {
                        prenatal prenatalPage = new prenatal();
                        prenatalPage.firstName = memberFNameText.Text.ToString();
                        prenatalPage.middleName = memberMNameText.Text.ToString();
                        prenatalPage.surname = memberSNameText.Text.ToString();
                        prenatalPage.employeeName = employeeName;
                        prenatalPage.employeeNumber = employeeNumber;
                        prenatalPage.familyNumber = familyNumber;
                        prenatalPage.displayInformation();

                        vaccine vaccinePage = new vaccine();
                        vaccinePage.firstName = memberFNameText.Text.ToString();
                        vaccinePage.middleName = memberMNameText.Text.ToString();
                        vaccinePage.surname = memberSNameText.Text.ToString();
                        vaccinePage.employeeName = employeeName;
                        vaccinePage.employeeNumber = employeeNumber;
                        vaccinePage.familyNumber = familyNumber;
                        vaccinePage.displayInformation();
                        
                        prenatalPage.Visible = true;
                        this.Visible = false;
                    }
                    if (checkUpTypeCombo.Text.ToString() == "Vaccine")
                        this.Visible = false;
                }


        }

       
    }
}
